package edu.scsu.csci.converter;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import edu.scsu.csci.IConverter;
import edu.scsu.csci.util.Utilities;


/**
 *
 * @author Maqbool Khan
 * @since jdk 1.5
 * @see
 * @viz.diagram SQLDateConverter.tpx
 */
final public class SQLDateConverter implements IConverter<java.sql.Date> {
	
	/**
	 * date formatter. Formats Strings from a "MM/dd/yyyy" to util date.
	 */
	public static final DateFormat simpleDateFormatter = new SimpleDateFormat("MM/dd/yyyy");
	
	/**
	 * date formatter. Formats Strings from a "MM/dd/yyyy HH:mm:ss" to util date.
	 */
	private static SimpleDateFormat sDatetimeFormatter=new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
	
	
	/**
	 * @see edu.scsu.csci.converter.IConverter#convert(java.lang.String, java.lang.Class)
	 */
	public java.sql.Date convert(String value, Class<?> type)
		throws ConversionException {
		if(Utilities.isNullOrEmpty(value)){
			throw new ConversionException( "String : " + value + " is null or empty.");
		}
		
		try
		{
			if(value.indexOf(":") != -1){
				return new java.sql.Date(sDatetimeFormatter.parse(value).getTime());
			}
			else if(value.indexOf(":") == -1 && value.indexOf(":") != -1){
				return new java.sql.Date(sDatetimeFormatter.parse(value).getTime());
			}
		}catch (ParseException e) {
			throw new ConversionException( "Unable to parse string " + value);
		}
		throw new ConversionException( "There is no date formatter defined to format string: " + value);
	}
}
